package flc.ast.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.ResumeBean;

public class SortAdapter extends RecyclerView.Adapter<SortAdapter.ViewHolder> {
    private List<ResumeBean.Module> mDatas;

    public SortAdapter(List<ResumeBean.Module> items) {
        mDatas = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_sort, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvName.setText(mDatas.get(position).getAlias());
        if (mDatas.get(position).isShow()) {
            holder.ivSel.setImageResource(R.drawable.aygx);
        } else {
            holder.ivSel.setImageResource(R.drawable.awxz3);
        }
        if (position == 0 || position == 1) {
            holder.ivSel.setImageResource(R.drawable.aygx1);
            holder.ivMove.setImageResource(R.drawable.abdsx2);
        } else {
            holder.ivMove.setImageResource(R.drawable.abdsx);
        }
        holder.ivSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == 0) return;
                if (mDatas.get(position).isShow()) {
                    mDatas.get(position).setShow(false);
                } else {
                    mDatas.get(position).setShow(true);
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    // 拖动排序逻辑
    public void onItemMove(int fromPos, int toPos) {
        Collections.swap(mDatas, fromPos, toPos);
        notifyItemMoved(fromPos, toPos);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        ImageView ivSel, ivMove;

        ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvSortItemName);
            ivSel = itemView.findViewById(R.id.ivSortItemSel);
            ivMove = itemView.findViewById(R.id.ivSortItemMove);
        }
    }
}
